package com.inspur.edp.common.type.deserializer;

import com.fasterxml.jackson.databind.JsonNode;
import com.inspur.edp.common.type.exception.TypeException;
import com.inspur.edp.common.type.utils.JsonSerializeUtil;

/**
 * @author lizhaorui
 * @date 2025/9/28
 * @description
 */
public class DefaultObjectDeserializer implements TypeDeserializer {

    private String className;

    private volatile Class cls;

    public DefaultObjectDeserializer(String className) {
        this.className = className;
    }

    @Override
    public Object deserialize(JsonNode jsonNode) {
        if (this.cls == null) {
            synchronized (DefaultObjectDeserializer.class) {
                if (this.cls == null) {
                    try {
                        this.cls = Class.forName(this.className);
                    } catch (ClassNotFoundException e) {
                        throw new TypeException("load class name occur error.", e);
                    }
                }
            }
        }
        return JsonSerializeUtil.toObject(jsonNode, this.cls);
    }

}